package descentFly.app.managePanel.chat
{
	import UIFree.controls.base.uif_source;
	import UIFree.uif_app;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import descentFly.app.df_component;
	import descentFly.app.df_label;
	import descentFly.server.df_servers;
	
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	public class df_chatItem extends df_turnItem {
		public static var DEFAULT_HEIGHT	: int = 30;
		public static var LOGO_SIZE			: int = 25;
		
		private var _title		: df_label;
		private var _logo		: uif_source;
		private var _message	: df_label;
		public function df_chatItem(parent:uif_component, user: Object, message: String, styleClass:Object='') {
			var vheight: int = DEFAULT_HEIGHT;
			var mheight: int = calcItemHeight(message);
			if (vheight < mheight) vheight = mheight;
			
			super(parent, vheight, styleClass);
			updateValues(user, message);
		}
		
		protected function updateValues(a_data: Object, message: String): void {
			_data = a_data;
			social.getUserImage(a_data, function(imageURL: String): void {
				setImage(imageURL);
			});
			social.getUserTitle(a_data, function(title: String): void {
				setTitle(title);
			});
			setMessage(message);
		}
		
		protected function setImage(imageURL: String): void {
			_logo.loadSource(imageURL);
		}
		
		protected function setTitle(title: String): void {
			_title.text = title;
		}
		
		protected function setMessage(message: String): void {
			_message.text 	= message;
			_message.height = _message.textField.textHeight * 1.2 + _message.getSpace(true) * 2;
			var vheight: int = _message.y + _message.height + getSpace(true) * 2;  
			if (vheight > height) height = vheight;
		}
		
		override protected function createComponents(layer:df_component, inner:Rectangle):void {
			_logo 		= new uif_source(layer, inner.x, inner.y, LOGO_SIZE, LOGO_SIZE, style.logo);
			inner.left += _logo.width + getSpace(true);
			_title 		= new df_label(layer, inner.x, inner.y, inner.width, 18, style.title);
			inner.top += _title.height + getSpace(true);
			_message 	= new df_label(layer, inner.x, inner.y, inner.width, inner.height, style.message);
		}
		
		protected static function calcItemHeight(message: String): int {
			return 20 + Math.ceil(message.length / 100) * 12;
		}
	}
}